package com.yunhe.authority.repository.authority;

import com.yunhe.authority.domain.authority.RealStation;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;


/**
 * @author zhengbo
 * @date 2021/5/11 15:37
 */
@Repository
public interface RealStationRepository extends Neo4jRepository<RealStation, Long> {

    @Override
    @Query("match(rt:RealStation) where id(rt)={id} return rt limit 1")
    Optional<RealStation> findById(@Param("id") Long id);

    @Query("match(rt:RealStation) where id(rt)={id} and rt.title={title} return rt limit 1")
    RealStation findByTitle(@Param("id") Long id, @Param("title") String title);

    /**
     * 根据总园区id与真实园区title查询真实园区
     * @param stationId 总园区ID
     * @param title 真实园区title
     */
    @Query("match(rt:RealStation)-[r:BELONG_TO_STATION{stationId:{stationId}}]->(station:Station) where rt.title={title} return rt ")
    RealStation findRealStationByStationIdAndTitle(@Param("stationId") Long stationId, @Param("title") String title);

    /**
     * 根据总园区id与真实园区全称查询真实园区
     * @param stationId 总园区ID
     * @param enterpriseFullName 真实园区全称
     */
    @Query("match(rt:RealStation)-[r:BELONG_TO_STATION{stationId:{stationId}}]->(station:Station) where rt.enterpriseFullName={enterpriseFullName} return rt ")
    RealStation findRealStationByStationIdAndEnterpriseFullName(@Param("stationId") Long stationId, @Param("enterpriseFullName") String enterpriseFullName);

    /**
     * 根据总园区id与真实园区简称查询真实园区
     * @param stationId 总园区ID
     * @param abbreviationName 真实园区简称
     */
    @Query("match(rt:RealStation)-[r:BELONG_TO_STATION{stationId:{stationId}}]->(station:Station) where rt.abbreviationName={abbreviationName} return rt ")
    RealStation findRealStationByStationIdAndAbbreviationName(@Param("stationId") Long stationId, @Param("abbreviationName") String abbreviationName);

    /**
     * 添加真实园区与总园区关系
     * @param stationId 总园区ID
     * @param id 真实园区ID
     */
    @Query("start station=node({stationId}),realStation=node({id}) create (realStation)-[r:BELONG_TO_STATION{stationId:{stationId}}]->(station)")
    void realStationBindStation(@Param("id") Long id, @Param("stationId") Long stationId);

    /**
     * 根据总园区id查询真实园区
     * @param stationId 总园区ID
     */
    @Query("match(rt:RealStation)-[r:BELONG_TO_STATION{stationId:{stationId}}]->(station:Station) return rt order by rt.sn asc,rt.createTime asc")
    Iterable<RealStation> findRealStationByStationId(@Param("stationId") Long stationId);

    /**
     * 添加真实园区与厂房关系
     * @param officeId 厂房ID
     * @param id 真实园区ID
     */
    @Query("start office=node({officeId}),realStation=node({id}) create (realStation)-[r:HAS_OFFICES{realStationId:{id}}]->(office)")
    void realStationBindOffice(@Param("id") Long id, @Param("officeId") Long officeId);


    /**
     * 添加真实园区与企业关系
     * @param companyId 企业ID
     * @param id 真实园区ID
     */
    @Query("start company=node({companyId}),realStation=node({id}) create (realStation)-[r:HAS_COMPANIES{realStationId:{id}}]->(company)")
    void realStationBindCompany(@Param("id") Long id, @Param("companyId") Long companyId);

    /**
     * 根据厂房id查询绑定的真实园区
     * @param officeId 厂房id
     * @return RealStation 0
     */
    @Query("match(rt:RealStation)-[r:HAS_OFFICES]->(o:Office) where id(o)={officeId} return rt")
    RealStation findRealStationByOfficeId(@Param("officeId") Long officeId);

    /**
     * 根据企业id查询绑定的真实园区
     * @param companyId 企业id
     * @return RealStation 0
     */
    @Query("match(rt:RealStation)-[r:HAS_COMPANIES]->(c:Company) where id(c)={companyId} return rt")
    RealStation findRealStationByCompanyId(@Param("companyId") Long companyId);

    @Query("match(rt:RealStation)-[r:HAS_OFFICES]->(o:Office) where id(o)={officeId} delete r")
    void unbindOfficeByOfficeId(@Param("officeId") Long officeId);
}
